package com.rc.saas.tenant.controller.printer;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.service.printer.ElecPrinterRecordService;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.elecPrinter.ElecPrinterRecordResultVo;
import com.rc.saas.tenant.vo.elecPrinter.ElecPrinterRecordSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;


/**
 * @author frog
 * @date 2021/8/6
 */
@Controller
@RequestMapping("/elecPrinterRecord")
public class ElecPrinterRecordController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ElecPrinterRecordService elecPrinterRecordService;


    /**
     * 跳转打印机记录页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/elecPrinter/elecPrinterRecord";
    }

    /**
     * 返回打印机列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageElecPrinterRecord", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageElecPrinterRecord(ElecPrinterRecordSearch elecPrinterRecordVO) {
        PageInfo<ElecPrinterRecordResultVo> pageInfo = elecPrinterRecordService.listPageElecPrinterRecord(elecPrinterRecordVO);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 导出小票打印记录
     */
    @ResponseBody
    @RequestMapping("/exportElecPrinterRecord")
    public TResponse exportElecPrinterRecord(ElecPrinterRecordSearch elecPrinterRecordVO, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        ExportUtils.setDefaultPaging(elecPrinterRecordVO);
        if (elecPrinterRecordVO.getCount() == 0 || elecPrinterRecordVO.getCount() == null) {
            elecPrinterRecordVO.setCount(elecPrinterRecordVO.getLimit());
        }

        //导出拼接开始条数
        elecPrinterRecordVO.setOffset(elecPrinterRecordVO.getLimit() * (elecPrinterRecordVO.getOffset() - 1));
        List<ElecPrinterRecordResultVo> list = elecPrinterRecordService.exportElecPrinterRecord(elecPrinterRecordVO);

        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("小票打印记录", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ElecPrinterRecordResultVo.class).sheet("小票打印记录").doWrite(list);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出小票打印记录失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }


}
